// pages/distribution/manage/list.js
var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../utils/user.js');
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        upAvatar:'',
        upNickName:'',
        allnum:0,
        lower1num:0,
        lower2num:0,
        list:[],
        vipList:[],
        userId:'201',
        UserNickName:'',
        UserAvatarUrl:'',
        money:0,
    },

    getUserInfoAll: function(callback){
        let userInfo = wx.getStorageSync('userInfo');
        this.setData({
            UserNickName : userInfo.nickName,
            UserAvatarUrl : userInfo.avatarUrl,
            userId : userInfo.userId
        })
    },

    getAllDate: function(userid){
        let alldata='';
        let upAvatar='0';
        let upNickName='0';
        let lower1num=0;
        let lower2num=0;
        let lower1=[];
        let lower2=[];
        let allnum=0;
        let list=[];
        wx.request({
          url: api.GetAllLower+"/"+this.data.userId,
            success :(res) => {
                alldata=res.data,
                 console.log(alldata);
                //获取下级数组
                lower1=alldata.data.lower1;
                //获取下下级数组
                lower2=alldata.data.lower2;
                //获取上级头像
                if(alldata.data.up!=null){
                    upAvatar=alldata.data.up.avatar;
                    upNickName=alldata.data.up.nickname;
                }
                //获取下级人数
                if(lower1!=null){
                     lower1num=lower1.length;
                }
                //获取下下级人数
                if(lower2!=null){
                    lower2num=lower2.length;
                }
                //获取总人数
                allnum=lower1num+lower2num;
                if(lower1!=null&&lower2!=null){
                    list=lower1.concat(lower2);
                }else if(lower1!=null){
                    list=lower1
                }else if(lower2!=null){
                    list=lower2
                }
                this.setData({
                    upAvatar : upAvatar,
                    upNickName : upNickName,
                    lower1num : lower1num,
                    lower2num : lower2num,
                    allnum : allnum,
                    list : list,
                })
                if (typeof callback === 'function') {
                    callback();
                  }
            }
        })
    },

    handleTap() {
        wx.showModal({
          title: '分销总览',
          content: '已邀请的下线'+this.data.lower1num+'人\r\n下线再邀请的人数'+this.data.lower2num+'人',
          showCancel: false
        });
      },

      navigateToNextPage: function() {
        wx.navigateTo({
          url: "/pages/checkout/goods/goods"
        })
      },

      getMoney(){
          wx.request({
            url: api.GetMoney+"/"+this.data.userId,
            success :(res) => {
                this.setData({
                    money : res.data.data[0]
                })
            }
          })
      },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getUserInfoAll();
        setTimeout(() => {
           this.getAllDate();
           this.getMoney();
          }, 300); // 延迟1秒执行
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})